package com.kupurui.asteacher.adapter;

import android.content.Context;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;

import com.android.frame.ui.BaseActivity;
import com.android.frame.util.Toolkit;
import com.android.frame.view.linearlistview.LinearListView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.bean.SelKnowledgeInfo;
import com.kupurui.asteacher.ui.job.JobSelAty;

import java.util.List;

/**
 * Created by Administrator on 2017/7/24.
 */

public class SelKnowledgeAdapter extends CommonAdapter<SelKnowledgeInfo.DownBean> {

    private SelKnowledgeAdapter adapter;
    BaseActivity activity;

    public SelKnowledgeAdapter(Context context, List<SelKnowledgeInfo.DownBean> mList, int itemLayoutId) {
        super(context, mList, itemLayoutId);
        activity = (BaseActivity) context;
    }

    @Override
    public void convert(final ViewHolder holder, final SelKnowledgeInfo.DownBean item,int positon) {
        LinearLayout linerlySubject = holder.getView(R.id.linerly_subject);
        TextView tvKnowledgeName = holder.getView(R.id.tv_knowledge_name);
        ImageView ivIcon = holder.getView(R.id.iv_icon);
        LinearLayout llNum = holder.getView(R.id.ll_num);
        TextView tvNum = holder.getView(R.id.tv_num);
        LinearListView listView = holder.getView(R.id.listview);
        listView.setVisibility(View.GONE);
        tvNum.setText(item.getCount());
        tvKnowledgeName.setText(item.getTitle());

        if (!Toolkit.listIsEmpty(item.getDown())) {
            llNum.setVisibility(View.GONE);
            adapter = new SelKnowledgeAdapter(mContext, item.getDown(), R.layout.sel_knowledge_item);
            listView.setAdapter(adapter);
        }else {
            llNum.setVisibility(View.VISIBLE);
            ivIcon.setVisibility(View.GONE);
        }

        if (item.isOpen()) {
            ivIcon.setImageResource(R.drawable.imgv_shrink);
            listView.setVisibility(View.VISIBLE);
        }else {
            listView.setVisibility(View.GONE);
            ivIcon.setImageResource(R.drawable.imgv_expand);
        }

        linerlySubject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Toolkit.listIsEmpty(item.getDown())){
                    item.setOpen(!item.isOpen());
                    notifyDataSetChanged();
                }else{
                    Bundle bundle = new Bundle();
                    bundle.putString("type","1");
                    bundle.putString("id",item.getId());
                    activity.startActivity(JobSelAty.class,bundle);
                }
            }
        });
    }

}
